package cfca.seal.dao.main.impl;

import cfca.seal.bean.TempFilePdf;
import cfca.seal.dao.SmartDao;
import cfca.seal.dao.main.ITempFilePdfDao;
import cfca.seal.exception.CodeException;
import cfca.seal.system.Env;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class TempFilePdfDao
  implements ITempFilePdfDao
{
  private static String tableName = "临时PDF文件";
  private static final Logger log = LoggerFactory.getLogger("web_sys");

  @Resource
  private SmartDao smartDao;

  public boolean insert(TempFilePdf tempFilePdf) throws CodeException {
    try { String sql = "INSERT INTO TEMP_FILE_PDF (ID, PDF_ID, TYPE, PDF_FILE, RESERVED_VALUE, REMARK1, CREATE_TIME) VALUES (?, ?, ?, ?, ?, ?, ?)";
      Object[] args = { tempFilePdf.getId(), tempFilePdf.getPdfId(), tempFilePdf.getType(), tempFilePdf.getPdfFile(), tempFilePdf.getReservedValue(), tempFilePdf.getRemark1(), tempFilePdf.getCreateTime() };

      log.debug("insert TEMP_FILE_PDF sql: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1002", tableName + "表插入失败", e);
    }
  }

  public boolean deleteByPdfId(String pdfId, String type) throws CodeException
  {
    try {
      String sql = "DELETE FROM TEMP_FILE_PDF WHERE PDF_ID = ? AND TYPE = ?";
      Object[] args = { pdfId, type };
      log.debug("delete TEMP_FILE_PDF sql by id: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }

  public TempFilePdf getInfoByPdfIdAndType(String pdfId, String type) throws CodeException
  {
    try {
      String sql = "SELECT * FROM TEMP_FILE_PDF WHERE PDF_ID = ? AND TYPE = ?";
      Object[] args = { pdfId, type };
      log.debug("SELECT TEMP_FILE_PDF sql by PdfIdAndType: " + sql + args);
      List list = this.smartDao.getList(TempFilePdf.class, sql, args);
      return (TempFilePdf)list.get(0);
    } catch (Exception e) {
      e.printStackTrace();
      log.error(e.getMessage());
      throw new CodeException("D1001", tableName + "表检索失败", e);
    }
  }

  public boolean deleteByCurrentTime(Date currentTime) throws Exception
  {
    try {
      String sql = "DELETE FROM TEMP_FILE_PDF WHERE ";
      if ("sqlite".equals(Env.databaseType))
        sql = sql + "DATETIME(substr(CREATE_TIME,0,11), 'unixepoch', 'localtime') >= DATETIME(?)";
      else if ("mysql".equals(Env.databaseType))
        sql = sql + "CREATE_TIME >= date_format(?,'%y-%m-%d %H:%i:%s')";
      else if ("oracle".equals(Env.databaseType))
        sql = sql + "CREATE_TIME >= to_date(?,'yyyy-mm-dd HH24:MI:SS')";
      else if ("db2".equals(Env.databaseType)) {
        sql = sql + "CREATE_TIME >= to_date(?,'yyyy-mm-dd hh24:mi:ss')";
      }

      Object[] args = { currentTime };
      log.debug("delete TEMP_FILE_PDF sql by currentTime: " + sql + args);
      int i = this.smartDao.update(sql, args);
      return i > 0;
    } catch (Exception e) {
      log.error(e.getMessage());
      throw new CodeException("D1004", tableName + "表删除失败", e);
    }
  }
}